# -*- coding: utf-8 -*-
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth import authenticate, login as django_login, logout as django_logout
from django.shortcuts import render_to_response, get_object_or_404
from settings import *
from bomberos.models import Bombero,Asociacion,Padron,Unidad
from django.views.generic import ListView,DetailView,CreateView,UpdateView
from bomberos.forms import BomberoForm,PadronForm
from django.views.generic.edit import ModelFormMixin
from django.core.urlresolvers import reverse


def login(request):
    error = None
    if request.method == 'POST':
        user = authenticate(username=request.POST['username'], password=request.POST['password'])
        if user is not None:
          if user.is_active:
            django_login(request, user)
            # success
            return HttpResponseRedirect(LOGIN_REDIRECT_URL)
          else:
          ## invalid login
           error = "Usuario/Password incorrectos."
        else:
          ## invalid login
           error = "Usuario/Password incorrectos."
          #return direct_to_template(request, 'invalid_login.html')
    return render_to_response('index.html', {'root_url': ROOT_URL,
                                                     'error': error},context_instance=RequestContext(request))

def logout(request):
    request.session.clear()
    django_logout(request)
    return HttpResponseRedirect(ROOT_URL)

def principal(request):
    return HttpResponseRedirect(LOGIN_REDIRECT_URL)

class ListPadronView(ListView):
        model = Padron
        context_object_name = 'padron'

class ListUnidadesView(ListView):
    model = Unidad
    context_object_name = 'unidades'


# Bomberos

class BomberoMixin(object):
    model = Bombero
    form_class = BomberoForm

    def get_success_url(self):
        return reverse('bomberos_list')
    def get_queryset(self):
        #return Bombero.objects.filter(padron__asociacion__pk=self.request.user.asociacion).filter(baja=False)
        return Bombero.objects.filter(baja=False)
        #return Bombero.objects.filter(owned_by=self.request.user)
    def form_valid(self, form):
        # save but don't commit the model form
        self.object = form.save(commit=False)
        # set the owner to be the current user
        #self.object.owned_by = self.request.user
        #
        # Here you can make any other adjustments to the model
        #
        self.object.save()
        # ok now call the base class and we are done.
        return super(ModelFormMixin, self).form_valid(form)

class ListBomberosView(BomberoMixin,ListView):
    model = Bombero
    context_object_name = 'bomberos'
class BomberoDetailView(BomberoMixin,DetailView):
    model = Bombero
    context_object_name = 'bomberos'
class BomberoCreateView(BomberoMixin,CreateView):
    model = Bombero
    context_object_name = 'bomberos'
class BomberoUpdateView(BomberoMixin,UpdateView):
    model = Bombero
    context_object_name = 'bomberos'

def darBajaBombero(request,pk):
    obj = get_object_or_404(Bombero,pk=pk)
    obj.baja = 'True'
    obj.save()
    return HttpResponseRedirect(reverse('bomberos_list'))

# Asociaciones

class AsociacionMixin(object):
    model = Asociacion
    #form_class = AsociacionForm

    def get_success_url(self):
        return reverse('asociaciones_list')
    def get_queryset(self):
        return Asociacion.objects.filter(baja=False)
        #return Bombero.objects.filter(owned_by=self.request.user)
    def form_valid(self, form):
        # save but don't commit the model form
        self.object = form.save(commit=False)
        # set the owner to be the current user
        #self.object.owned_by = self.request.user
        #
        # Here you can make any other adjustments to the model
        #
        self.object.save()
        # ok now call the base class and we are done.
        return super(ModelFormMixin, self).form_valid(form)

class ListAsociacionesView(AsociacionMixin,ListView):
    model = Asociacion
    context_object_name = 'asociaciones'
class AsociacionDetailView(AsociacionMixin,DetailView):
    model = Asociacion
    context_object_name = 'asociaciones'
class AsociacionCreateView(AsociacionMixin,CreateView):
    model = Asociacion
    context_object_name = 'asociaciones'
class AsociacionUpdateView(AsociacionMixin,UpdateView):
    model = Asociacion
    context_object_name = 'asociaciones'

def darBajaAsociacion(request,pk):
    obj = get_object_or_404(Asociacion,pk=pk)
    obj.baja = 'True'
    obj.save()
    return HttpResponseRedirect(reverse('asociaciones_list'))


# Unidades

class UnidadMixin(object):
    model = Unidad
    #form_class = AsociacionForm

    def get_success_url(self):
        return reverse('unidades_list')
    def get_queryset(self):
        return Unidad.objects.all()
        #return Bombero.objects.filter(owned_by=self.request.user)
    def form_valid(self, form):
        # save but don't commit the model form
        self.object = form.save(commit=False)
        # set the owner to be the current user
        #self.object.owned_by = self.request.user
        #
        # Here you can make any other adjustments to the model
        #
        self.object.save()
        # ok now call the base class and we are done.
        return super(ModelFormMixin, self).form_valid(form)

class ListUnidadesView(UnidadMixin,ListView):
    model = Unidad
    context_object_name = 'unidades'
class UnidadDetailView(UnidadMixin,DetailView):
    model = Unidad
    context_object_name = 'unidades'
class UnidadCreateView(UnidadMixin,CreateView):
    model = Unidad
    context_object_name = 'unidades'
class UnidadUpdateView(UnidadMixin,UpdateView):
    model = Unidad
    context_object_name = 'unidades'

def darBaja(request,pk):
    obj = get_object_or_404(Unidad,pk=pk)
    obj.observaciones = 'BAJA'
    obj.save()
    return HttpResponseRedirect(reverse('unidades_list'))


# Padron Bomberos

class PadronMixin(object):
    model = Padron
    form_class = PadronForm

    def get_success_url(self):
        return reverse('padron_list')
    def get_queryset(self):
        return Padron.objects.all()
        #return Bombero.objects.filter(owned_by=self.request.user)
    def form_valid(self, form):
        # save but don't commit the model form
        self.object = form.save(commit=False)
        # set the owner to be the current user
        #self.object.owned_by = self.request.user
        #
        # Here you can make any other adjustments to the model
        #
        self.object.save()
        # ok now call the base class and we are done.
        return super(ModelFormMixin, self).form_valid(form)

class ListPadronView(PadronMixin,ListView):
    model = Padron
    context_object_name = 'padron'
class PadronDetailView(PadronMixin,DetailView):
    model = Padron
    context_object_name = 'padron'
class PadronCreateView(PadronMixin,CreateView):
    model = Padron
    context_object_name = 'padron'
class PadronUpdateView(PadronMixin,UpdateView):
    model = Padron
    context_object_name = 'padron'
